#ifndef _GLOBAL_H
#define _GLOBAL_H
#include <assert.h>
#include <stdlib.h>
#include "type_redefine.h"
//定义结构体的时候要注意字节对齐,数据结构的定义在各个数据结构的头文件中声明，不在global中声明


/* 日志打印系统 */
// 在mobaxterm中打印
#define TRACE_MOBAXTERM

// 仅warning/error信息输出
// #define TRACE_JUST_WARNING_AND_ERROR

#ifdef TRACE_MOBAXTERM

// 38前景色 48背景色 后面跟着;5; %dm %d是色号(1-255)
// #define LOG_COLOUR_WHITE           "\x1b[38;5;37m"
#define LOG_COLOUR_BLUE            "\x1b[38;5;87m"
#define LOG_COLOUR_GREEN           "\x1b[38;5;02m"
#define LOG_COLOUR_YELLOW          "\x1b[38;5;220m"
#define LOG_COLOUR_RED             "\x1b[38;5;01m"

#define LOG_COLOUR_RESET           "\x1b[0m"
#else
#define LOG_COLOUR_BLUE            ""
#define LOG_COLOUR_GREEN           ""
#define LOG_COLOUR_YELLOW          ""
#define LOG_COLOUR_RED             ""

#define LOG_COLOUR_RESET           ""
#endif

enum TRACE_LEVEL_EM
{
    NONE,                            // 裸打印printf
    DEBUG,
    INFO, 
    WARNING,
    ERROR
};

#ifndef TRACE_JUST_WARNING_AND_ERROR
#define TRACE(LEVEL, format, ...) \
        do                        \
        {                         \
            switch (LEVEL)        \
            {                     \
                case NONE:        \
                {                 \
                    printf(format, ##__VA_ARGS__);                                                  \
                }                                                                                   \
                break;                                                                              \
                case DEBUG:                                                                         \
                {                                                                                   \
                    printf("%s[DEBUG]: %s %d %s: " format"\n",LOG_COLOUR_BLUE, __FUNCTION__, __LINE__, LOG_COLOUR_RESET, ##__VA_ARGS__);    \
                }                                                                                   \
                break;                                                                              \
                case INFO:                                                                          \
                {                                                                                   \
                    printf( "%s[INFO]: %s %s %d %s: " format"\n", LOG_COLOUR_GREEN, __FILE__, __FUNCTION__, __LINE__, LOG_COLOUR_RESET, ##__VA_ARGS__);    \
                }                                                                                   \
                break;                                                                              \
                case WARNING:                                                                       \
                {                                                                                   \
                    printf( "%s[WARNING]: %s %s %d %s: " format"\n", LOG_COLOUR_YELLOW, __FILE__, __FUNCTION__, __LINE__, LOG_COLOUR_RESET, ##__VA_ARGS__);\
                }                                                                                   \
                break;                                                                              \
                case ERROR:                                                                         \
                {                                                                                   \
                    printf( "%s[ERROR]: %s %s %d %s: " format"\n", LOG_COLOUR_RED,__FILE__, __FUNCTION__, __LINE__, LOG_COLOUR_RESET,  ##__VA_ARGS__); \
                }                                                                                       \
                break;                                                                                  \
                                                                                                        \
                default:                                                                                \
                {                                                                                       \
                    printf(LOG_COLOUR_RED "%s %s %d Debug level error!!!\n\n\n"LOG_COLOUR_RESET, __FILE__, __FUNCTION__, __LINE__); \
                }                                                                                                                   \
                break;                                                                                                              \
            }                                                                                                                       \
        } while (0)                                                                                                                 \
        
#else
#define TRACE(LEVEL, format, ...) \
        do                        \
        {                         \
            switch (LEVEL)          \
            {                       \
            case WARNING:           \
            {                                                                                 \
                printf( "%s[WARNING]: %s" format"\n", LOG_COLOUR_YELLOW, LOG_COLOUR_RESET, ##__VA_ARGS__);\
            }                                                                                   \
            break;                                                                              \
            case ERROR:                                                                         \
            {                                                                                   \
                printf( "%s[ERROR]: %s %s %d %s" format"\n", LOG_COLOUR_RED,__FILE__, __FUNCTION__, __LINE__, LOG_COLOUR_RESET,  ##__VA_ARGS__); \
            }                                                                                   \
            break;                                                                              \
                                                                                                \
            default:                                                                            \
            break;                                                                              \
            }                                                                                   \
        } while (0)                                                                             \
        
#endif





//常用的宏定义
#define DIM(arr)	(sizeof(arr)/sizeof(arr[0]))
#define TABLESIZE(arr) DIM(arr)
#define OFFSET(ST,mem)	( &(ST*)0->mem )	
#define SWAP(x, y) do {    \
    typeof(x) _temp = (x); \
    (x) = (y);             \
    (y) = _temp;           \
} while (0)
#define FREE(p) (free(p),p=NULL)
#define MALLOC  malloc

#define INTERROR 0x7fffffff

#endif